package com.unidt.www.framework.common.util.number;


import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 功能描述
 *
 * @author ZKJ
 * @date 2021/6/15
 */
public class VMathUtil {

    /**
     * 获取增长百分比
     *
     * @param
     * @return
     * @author zkj
     * @date 2021/6/15
     */

    public static Double getPercentage(Object a, Object b) {
        return getPercentage(a, b, 2);
    }

    /**
     * 最大100% 限制
     *
     * @param a
     * @param b
     * @param max
     * @return
     */
    public static Double getPercentage(Object a, Object b, boolean max) {
        Double percent = getPercentage(a, b, 2);
        if (max) {
            if (Objects.nonNull(percent)) {
                if (new Double("100").compareTo(percent) >= 0) {
                    return percent;
                } else {
                    return new Double("100");
                }
            }
        }
        return percent;
    }

    public static Integer multiplication(Object a, Object b) {
        if (Objects.isNull(a)) {
            a = "0";
        }
        if (Objects.isNull(b)) {
            b = "0";
        }
        BigDecimal ad = new BigDecimal(String.valueOf(a));
        BigDecimal bd = new BigDecimal(String.valueOf(b));
        return ad.multiply(bd).intValue();
    }


    public static Double multip(Object a, Object b) {
        if (Objects.isNull(a)) {
            a = "0";
        }
        if (Objects.isNull(b)) {
            b = "0";
        }
        BigDecimal ad = new BigDecimal(String.valueOf(a));
        BigDecimal bd = new BigDecimal(String.valueOf(b));
        return ad.multiply(bd).doubleValue();
    }


    /**
     * 功能描述:
     *
     * @param a
     * @param b
     * @param scale 保留小树的位数量
     * @return java.lang.Double
     * @author zkj
     * @date 2021/11/16
     */

    public static Double getPercentage(Object a, Object b, Integer scale) {
        if (Objects.isNull(a)) {
            a = "0";
        }
        if (Objects.isNull(b)) {
            b = "0";
        }
        BigDecimal ad = new BigDecimal(String.valueOf(a));
        BigDecimal bd = new BigDecimal(String.valueOf(b));

        if (new BigDecimal(0).compareTo(ad) == 0) {
            return new Double("0");
        } else if (new BigDecimal(0).equals(bd)) {
            return new Double("0");
        } else {
            return bd.divide(ad, 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
    }


    public static String getPercentageStr(Object a, Object b) {
        Double percentage = getPercentage(a, b);
        return percentage.toString() + "%";
    }

    public static Double subtract(Object now, Object old) {
        if (Objects.isNull(now)) {
            now = "0";
        }

        if (Objects.isNull(old)) {
            old = "0";
        }
        BigDecimal ad = new BigDecimal(String.valueOf(now));
        BigDecimal bd = new BigDecimal(String.valueOf(old));
        BigDecimal diff = ad.subtract(bd);
        return diff.doubleValue();
    }




    public static Double subtractLowLimit(Object now, Object old,Double limit) {
        if (Objects.isNull(now)) {
            now = "0";
        }

        if (Objects.isNull(old)) {
            old = "0";
        }
        BigDecimal ad = new BigDecimal(String.valueOf(now));
        BigDecimal bd = new BigDecimal(String.valueOf(old));
        BigDecimal diff = ad.subtract(bd);

       Double  diffs   = diff.doubleValue();
       if(diffs.compareTo(limit)==-1){
           return  limit;
       }else {
           return  diffs;
       }
    }

    public static Double getIncreasePercentage(Object now, Object old) {

        if (Objects.isNull(now)) {
            now = "0";
        }

        if (Objects.isNull(old)) {
            old = "0";
        }
        BigDecimal ad = new BigDecimal(String.valueOf(now));
        BigDecimal bd = new BigDecimal(String.valueOf(old));

        if (new BigDecimal(0).compareTo(ad) == 0 && new BigDecimal(0).compareTo(bd) != 0) {
            return new Double("100");
        } else {
            BigDecimal diff = ad.subtract(bd);

            if (diff.compareTo(new BigDecimal(0)) == 0) {
                return new Double("0");
            }
            Double p = diff.divide(ad, 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).doubleValue();
            return p;
        }

    }


    public static Double divide(Object a, Object b, Integer scale) {

        if (Objects.isNull(a)) {
            a = "0";
        }

        if (Objects.isNull(b)) {
            b = "0";
        }
        BigDecimal ad = new BigDecimal(String.valueOf(a));
        BigDecimal bd = new BigDecimal(String.valueOf(b));

        Double p = ad.divide(bd, scale, BigDecimal.ROUND_HALF_UP).doubleValue();

        return p;

    }


    public static String getIncreasePercentageStr(Object now, Object old) {

        Double percentage = getIncreasePercentage(now, old);
        return percentage.toString() + "%";

    }

    public static Integer getInt(String str) {

        if (StringUtils.isNotBlank(str)) {
            while (str.startsWith("0")) {
                Integer length = str.length();
                if (length == 1 && str.equals("0")) {
                    return new Integer(0);
                }
                str = str.substring(1, length);
            }
            return Integer.valueOf(str);
        }
        return new Integer(0);
    }


    public static String completionStr(Integer number, Integer length) {
        String str = String.valueOf(number);
        while (str.length() < length) {
            str = "0" + str;
        }
        return str;
    }

    public static String completionStr(Long number, Integer length) {
        String str = String.valueOf(number);
        while (str.length() < length) {
            str = "0" + str;
        }
        return str;
    }

    public static Double add(Double v1, Double v2) {
        if (Objects.isNull(v1)) {
            v1 = new Double(0);
        }
        if (Objects.isNull(v2)) {
            v2 = new Double(0);
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).doubleValue();
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }


    public static Integer getRandom(Random random, Integer max, Integer min) {
        if (Objects.isNull(random)) {
            random = new Random(10);
        }
        return random.nextInt(max) % (max - min) + min;
    }

    /**
     * 获取中位数
     * 前提条件 list 已经排序
     *
     * @param list
     * @return
     */
    public static Integer getMidValue(List<Integer> list) {

        if (list == null || list.isEmpty()) {
            return null;
        }
        Integer size = list.size();
        if (size == 1) {
            return list.get(0);
        }
        Integer midIndex = divide(size, 2, 0).intValue();
        return list.get(midIndex);

    }

    /**
     * 功能描述: 取 数字的前 位数
     *
     * @param number
     * @param frontNum
     * @return java.lang.Long
     * @author zkj
     * @date 2022/5/24
     */
    public static Long front(Long number, Integer frontNum) {
        if (Objects.isNull(number)) {
            return null;
        }
        String s = String.valueOf(number);
        if (s.length() <= frontNum) {
            return Long.valueOf(s);
        } else {
            s = s.substring(0, frontNum);
            return Long.valueOf(s);
        }

    }

    public static Long after(Long number, Integer after) {
        if (Objects.isNull(number)) {
            return null;
        }
        String s = String.valueOf(number);
        if (s.length() <= after) {
            return Long.valueOf(s);
        } else {
            s = s.substring(s.length()-after, s.length());
            return Long.valueOf(s);
        }

    }


    /**
     * 功能描述: 取前几位 不满足返回null
     * @author zkj
     * @date 2022/7/12
     * @param number
     * @param frontNum
     * @return java.lang.Long
     */
    public static Long limit(Long number, Integer frontNum) {
        if (Objects.isNull(number)) {
            return null;
        }
        String s = String.valueOf(number);
        if (s.length() <= frontNum) {
            return null;
        } else {
            s = s.substring(0, frontNum);
            return Long.valueOf(s);
        }

    }


    public static void main(String[] args) {

        System.out.println(after(Long.valueOf(123456),3));


    }


}
